package com.adwin.addcoin.repository.net

import android.text.TextUtils
import android.util.Log
import com.adwin.addcoin.AddCoinApplication
import com.adwin.addcoin.BuildConfig
import com.adwin.addcoin.utils.DevicesUtils
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Response
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory

/**
 * 单例网络访问框架
 */
class RetrofitClient {
    companion object {
        fun getInstance() = InstanceHolder.INSTANCE
    }

    object InstanceHolder {
        val INSTANCE = RetrofitClient()
    }

    private var retrofit: Retrofit? = null

    @Synchronized
    private fun getRetrofit(): Retrofit? {
        if (null == retrofit) {
            val loggingInterceptor = HttpLoggingInterceptor()
            if(BuildConfig.DEBUG){
                loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
            }else{
                loggingInterceptor.level = HttpLoggingInterceptor.Level.NONE
            }
            retrofit = Retrofit.Builder().client(OkHttpClient.Builder().addInterceptor(object : Interceptor{
                override fun intercept(chain: Interceptor.Chain): Response {
                    if (chain.request().url().toString().indexOf("/pine/freehand/atomic") != -1){
                        val builder = chain.request().newBuilder()
                        val request = builder.build()
                        return chain.proceed(request)
                    }else if (!TextUtils.isEmpty(AddCoinApplication.token)){
                        val builder = chain.request().newBuilder()
                        val host = chain.request().url().url().host
                        if (BuildConfig.ReportUrl.indexOf(host) == -1){
                            builder.addHeader("Authorization","Bearer ${AddCoinApplication.token}")
                            if (AddCoinApplication.isCloak){
                                builder.addHeader("X-App-R-Version","noId")
                            }
                            builder.addHeader("app-Version","${DevicesUtils.map[DevicesUtils.versionName]?:""}")
                            builder.addHeader("Accept-Language","${DevicesUtils.map[DevicesUtils.language]?:"en"}")
                            builder.addHeader("X-App-B-Name","${DevicesUtils.map[DevicesUtils.packageName]}")
                        }
                        val request = builder.build()
                        return chain.proceed(request)
                    }
                    return chain.proceed(chain.request())
                }
            }
            ).addInterceptor(loggingInterceptor).build())
                .baseUrl(BuildConfig.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .build()
        }

        return retrofit
    }

    fun <T> getService(tClass: Class<T>): T {
        return getRetrofit()!!.create(tClass)
    }
}